package com.jc.service.jctask;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.jc.cds.model.CustomJcTask;
import com.jc.ds.base.service.BaseServiceImpl;
import com.jc.ds.mapper.BaseCustomMapper;
import com.jc.ds.model.JcTask;
import com.jc.framework.enums.Booleans;
import com.jc.framework.enums.TaskType;


@Service
public class JcTaskServiceImpl extends BaseServiceImpl<JcTask, CustomJcTask, Long> implements JcTaskService {

	@Override
	public BaseCustomMapper<CustomJcTask, Long> getCustomMapper() {
		return null;
	}

	public int addTask(TaskType taskType,String businessId,String businessData,Booleans bool,String remark) {
		JcTask extisTask = new JcTask();
		extisTask.setTaskType(taskType.getName());
		extisTask.setTaskBusinessId(businessId);
		List<JcTask> jcTaskList = queryAll(extisTask);
		if(CollectionUtils.isNotEmpty(jcTaskList)) {
			JcTask task = jcTaskList.get(0);
			task.setLastUpdateTime(new Date());
			int  exeCount = 0;
			if(task.getExeCount() != null) {
				exeCount = task.getExeCount() + 1;
			}
			if(exeCount > task.getMaxExeCount()) {
				return 0;
			}
			task.setExeCount(exeCount); //累加执行次数
			task.setTaskType(taskType.getName());
			task.setTaskBusinessId(businessId);
			task.setTaskData(businessData);
			task.setTaskExeTag(bool.getName());
			if(StringUtils.isNotBlank(remark))task.setExtend1(remark);
			return this.modify(task, false);
		}else {
			JcTask task = new JcTask();
			task.setTaskType(taskType.getName());
			task.setTaskBusinessId(businessId);
			task.setTaskData(businessData);
			task.setTaskExeTag(bool.getName());
			task.setCreateTime(new Date());
			task.setLastUpdateTime(new Date());
			task.setExeCount(1);
			if(StringUtils.isNotBlank(remark))task.setExtend1(remark);
			return add(task, false);
		}
	}
	
	
	
	
}
